package com.hejing.io流概述.处理流之转换流;

import org.junit.Test;

import java.io.*;

/**
 * ClassName: InOutStreamReaderWriterTest
 * Package: com.hejing.io流概述.处理流之转换流
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-06-01 21:15
 * @Version: 1.0
 */
public class InOutStreamReaderWriterTest {
    /**
     * 文件 dbcp_utf-8.txt 使用的编码方式是 utf-8
     */
    @Test
    public void test01(){
        InputStreamReader isr = null;
        try {
            // 1、创建File对象。
            File file1 = new File("D:\\HeJing_Java\\io_test\\dbcp_utf-8.txt");

            // 2、创建流对象
            FileInputStream fis = new FileInputStream(file1);
            BufferedInputStream bis = new BufferedInputStream(fis);
            // isr = new InputStreamReader(bis);  // 此时使用的是IDEA默认的UTF-8的字符集。
            // isr = new InputStreamReader(bis, "gbk");  // 显示使用gbk字符集, 输出乱码
            isr = new InputStreamReader(bis, "utf-8");  // 显示使用utf-8字符集

            // 读入操作
            char[] cBuffer = new char[1024];
            int size;
            while((size = isr.read(cBuffer)) != -1) {
                String str = new String(cBuffer, 0, size);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源:
            try {
                if(isr != null)
                    isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 文件 dbcp_gbk.txt使用的编码方式是 gbk
     */
    @Test
    public void test02(){
        InputStreamReader isr = null;
        try {
            // 1、创建File对象。
            File file1 = new File("D:\\HeJing_Java\\io_test\\dbcp_gbk.txt");

            // 2、创建流对象
            FileInputStream fis = new FileInputStream(file1);
            BufferedInputStream bis = new BufferedInputStream(fis);
            // isr = new InputStreamReader(bis);  // 此时使用的是IDEA默认的UTF-8的字符集,出现乱码

            isr = new InputStreamReader(bis, "gbk");  // 显示使用gbk字符集

            // 读入操作
            char[] cBuffer = new char[1024];
            int size;
            while((size = isr.read(cBuffer)) != -1) {
                String str = new String(cBuffer, 0, size);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源:
            try {
                if(isr != null)
                    isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 需求: 将gbk格式的文件转换为 utf-8格式的文件。
     */
    @Test
    public void test03(){
        File file1 = new File("D:\\HeJing_Java\\io_test\\dbcp_gbk.txt");
        File file2 = new File("D:\\HeJing_Java\\io_test\\dbcp_gbk_to_utf-8.txt");

        InputStreamReader isr = null;
        OutputStreamWriter osw = null;

        try{
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            isr = new InputStreamReader(bis, "gbk");
            osw = new OutputStreamWriter(bos, "utf-8");

            char[] cBuffer = new char[1024];
            int size = -1;
            while((size = isr.read(cBuffer)) != -1) {
                osw.write(cBuffer, 0, size);
            }
            System.out.println("复制成功...");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}




























